#include "recovery/fs/file/FileLogOperation.h"

#include "recovery/fs/file/File.h"

namespace recovery {
namespace fs {
namespace file {

bool FileLogOperation::exist(const std::string & path){
    File f(path);
    return f.exist();
}

ErrnoStatus FileLogOperation::openExist(const std::string & path, std::unique_ptr<FileLogOperation> & operation){
    operation.reset(new FileLogOperation(path));
    return operation->file().openForRandomWrite(operation->writableFd());
}
ErrnoStatus FileLogOperation::create(const std::string & path, std::unique_ptr<FileLogOperation> & operation){
    operation.reset(new FileLogOperation(path));
    return operation->file().createForRandomWrite(operation->writableFd());
}

FileLogOperation::FileLogOperation(const std::string & path)
: AbstractFileOperation(path, true)
{
}
FileLogOperation::~FileLogOperation(){
}

ErrnoStatus FileLogOperation::write(const uint8_t * buffer, const size_t len, size_t & writedBytes){
    return file().write(fd(), buffer, len, writedBytes);
}
ErrnoStatus FileLogOperation::write(const std::string & data, size_t & writedBytes){
    return file().write(fd(), reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), writedBytes);
}
ErrnoStatus FileLogOperation::positionedRead(const size_t offset, uint8_t * buffer, const size_t len, size_t & readBytes) const{
    return file().positionedRead(fd(), offset, buffer, len, readBytes);
}
ErrnoStatus FileLogOperation::positionedWrite(const size_t offset, const uint8_t * buffer, const size_t len, size_t & writedBytes) const{
    return file().positionedWrite(fd(), offset, buffer, len, writedBytes);
}
ErrnoStatus FileLogOperation::sync() const{
    return file().sync(fd());
}
ErrnoStatus FileLogOperation::size(size_t & sz) const{
    return file().size(sz);
}
ErrnoStatus FileLogOperation::remove() const {
    return file().remove();
}
ErrnoStatus FileLogOperation::truncate(size_t size) {
    return file().truncate(size);
}
ErrnoStatus FileLogOperation::closeOnce(){
    return file().close(fd());
}
ErrnoStatus FileLogOperation::seekEnd(){
    return file().seekEnd(fd(),  0);
}

} //end of namespace file
} //end of namespace fs
} //end of namespace recovery
